<?php
function defineDate($str){
	$dd='';$mm='';$mmm='';$yyyy='';$time='';$hh='';$mi='';$ss='';
				  
	$arr_mon	= array('', 'JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL', 'AUG', 'SEP', 'OCT', 'NOV', 'DEC');
	$arr_month	= array('', 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');
	$arr_bulan	= array('', 'Januari', 'Februari', 'Maret', 'April', 'Mei', 'Juni', 'Juli', 'Agustus', 'September', 'Oktober', 'Nopember', 'Desember');
	$str		= trim($str);
	$arr_date	= array();
	$format		= '';
	$delimiter	= '';
	
	// Separating Date & Time
	$arr_temp	= explode(' ', $str);
	$str		= $arr_temp[0];
	$time		= (count($arr_temp)>1) ? $arr_temp[1] : '';
	unset($arr_temp);
	
	// Explode Part of Time
	$hh = '00'; $mi = '00'; $ss = '00';
	if($time!='' && @strstr($time,':')!=''){
		$arr_time	= explode(':', $time);
		$hh			= trim($arr_time[0]!='') ? $arr_time[0] : '00';
		$mi			= trim($arr_time[1]!='') ? $arr_time[1] : '00';
		$ss			= trim($arr_time[2]!='') ? $arr_time[2] : '00';
	}
	
	// Explode Part of Date
	if(strlen($str)>=8 && strlen($str)<=11){
		if(@strstr($str, '/')!=''){
			$arr_date	= explode('/', $str);
			$delimiter	= '/';
		}else
		if(@strstr($str, '-')!=''){
			$arr_date	= explode('-', $str);
			$delimiter	= '-';
		}
	}
	
	if(count($arr_date) == 3 && $delimiter != ''){
		
		// ----------- [ DD/MM/YYYY ] or [ DD-MM-YYYY ]
		if(	intval($arr_date[0])>0 && intval($arr_date[0])<32 && 
			intval($arr_date[1])>0 && intval($arr_date[1])<13 && 
			intval($arr_date[2])>=1900){
				$format	= 'DD' . $delimiter . 'MM' . $delimiter . 'YYYY';
				$dd		= (intval($arr_date[0])<10 ? '0' : '') . intval($arr_date[0]);
				$mm		= (intval($arr_date[1])<10 ? '0' : '') . intval($arr_date[1]);
				$mmm	= $arr_mon[ intval($arr_date[1]) ];
				$yyyy	= intval($arr_date[2]);
		}
		
		// ----------- [ YYYY/MM/DD ] or [ YYYY-MM-DD ]
		if(	intval($arr_date[2])>0 && intval($arr_date[2])<32 && 
			intval($arr_date[1])>0 && intval($arr_date[1])<13 && 
			intval($arr_date[0])>=1900){
				$format	= 'YYYY' . $delimiter . 'MM' . $delimiter . 'DD';
				$dd		= (intval($arr_date[2])<10 ? '0' : '') . intval($arr_date[2]);
				$mm		= (intval($arr_date[1])<10 ? '0' : '') . intval($arr_date[1]);
				$mmm	= $arr_mon[ intval($arr_date[1]) ];
				$yyyy	= intval($arr_date[0]);
		}
		
		// ----------- [ DD/MMM/YYYY ] or [ DD-MMM-YYYY ]
		if(	intval($arr_date[0])>0 && intval($arr_date[0])<32 && 
			!is_numeric($arr_date[1]) && in_array($arr_date[1], $arr_mon) && 
			intval($arr_date[2])>=1900){
				$format	= 'DD' . $delimiter . 'MMM' . $delimiter . 'YYYY';
				$dd		= (intval($arr_date[0])<10 ? '0' : '') . intval($arr_date[0]);
				$mm		= array_search(strtoupper($arr_date[1]), $arr_mon);
				$mm		= (intval($mm)<10 ? '0' : '') . $mm;
				$mmm	= strtoupper($arr_date[1]);
				$yyyy	= intval($arr_date[2]);
		}
		
		// ----------- [ YYYY/MMM/DD ] or [ YYYY-MMM-DD ]
		if(	intval($arr_date[2])>0 && intval($arr_date[2])<32 && 
			!is_numeric($arr_date[1]) && in_array($arr_date[1], $arr_mon) && 
			intval($arr_date[0])>=1900){
				$format	= 'YYYY' . $delimiter . 'MMM' . $delimiter . 'DD';
				$dd		= (intval($arr_date[2])<10 ? '0' : '') . intval($arr_date[2]);
				$mm		= array_search(strtoupper($arr_date[1]), $arr_mon);
				$mm		= (intval($mm)<10 ? '0' : '') . $mm;
				$mmm	= strtoupper($arr_date[1]);
				$yyyy	= intval($arr_date[0]);
		}
		
		// ----------- [ DD/MMM/RR ] or [ DD-MMM-RR ]
		if(	intval($arr_date[0])>0 && intval($arr_date[0])<32 && 
			!is_numeric($arr_date[1]) && in_array($arr_date[1], $arr_mon) && 
			intval($arr_date[2])<100){
				$format	= 'DD' . $delimiter . 'MMM' . $delimiter . 'RR';
				$dd		= (intval($arr_date[0])<10 ? '0' : '') . intval($arr_date[0]);
				$mm		= array_search(strtoupper($arr_date[1]), $arr_mon);
				$mm		= (intval($mm)<10 ? '0' : '') . $mm;
				$mmm	= strtoupper($arr_date[1]);
				
				$pre_year	= substr(@date('Y'),0, 2);
				$suf_year	= substr(@date('Y'),2, 2);
				$yyyy	= (intval($arr_date[2])<50 ? $pre_year : intval($pre_year-1)) . (intval($arr_date[2])<10 ? '0' : '') . intval($arr_date[2]);
		}else
		
		// ----------- [ RR/MMM/DD ] or [ RR-MMM-DD ]
		if(	intval($arr_date[2])>0 && intval($arr_date[2])<32 && 
			!is_numeric($arr_date[1]) && in_array($arr_date[1], $arr_mon) && 
			intval($arr_date[0])<100){
				$format	= 'RR' . $delimiter . 'MMM' . $delimiter . 'DD';
				$dd		= (intval($arr_date[2])<10 ? '0' : '') . intval($arr_date[2]);
				$mm		= array_search(strtoupper($arr_date[1]), $arr_mon);
				$mm		= (intval($mm)<10 ? '0' : '') . $mm;
				$mmm	= strtoupper($arr_date[1]);
				
				$pre_year	= substr(@date('Y'),0, 2);
				$suf_year	= substr(@date('Y'),2, 2);
				$yyyy	= (intval($arr_date[0])<50 ? $pre_year : intval($pre_year-1)) . (intval($arr_date[0])<10 ? '0' : '') . intval($arr_date[0]);
		}
		
	}
	
	unset($arr_date);
	return	array('format'	=> $format,
				  'dd'		=> $dd,
				  'mm'		=> $mm,
				  'mmm'		=> $mmm,
				  'mon'		=> $arr_mon[ intval($mm) ],
				  'month'	=> $arr_month[ intval($mm) ],
				  'bulan'	=> $arr_bulan[ intval($mm) ],
				  'yyyy'	=> $yyyy,
				  'rr'		=> substr($yyyy,2,2),
				  'time'	=> $time,
				  'hh'		=> $hh,
				  'mi'		=> $mi,
				  'ss'		=> $ss);
	
}

function is_date($str){
	
	$arr_result	= defineDate($str);
	return (trim($arr_result['format']) == '') ? false : true;
	
}

function checkDateFormat($str){
	$arr_result	= defineDate($str);
	return $arr_result['format'];
}

if(!function_exists('date_format3')){
function date_format3($str, $format){
	if($str == NULL || trim($str) == '') return ''; 
	$result	= strtoupper($format);
	
	$arr_result	= defineDate($str);
	if(count(array_keys($arr_result)) == 0) return ''; 
	
	// Set day
	if(@strstr($format,'DAY')!='' || @strstr($format,'HARI')!=''){
		$arr_day	= array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');
		$arr_hari	= array('Minggu', 'Senin', 'Selasa', 'Rabu', 'Kamis', 'Jumat', 'Sabtu');
		
		$timestamp	= @strtotime($arr_result['yyyy'].'-'.$arr_result['mm'].'-'.$arr_result['dd'], @date("Y-m-d"));
		$li_dow		= @date("w", $timestamp);
		
		$result	= str_replace('DAY', $arr_day[$li_dow], $result);
		$result	= str_replace('HARI', $arr_hari[$li_dow], $result);
	}
	
	// Set format
	$result	= str_replace('DD', $arr_result['dd'], $result);
	$result	= str_replace('MM', $arr_result['mm'], $result);
	$result	= str_replace('MMM', $arr_result['mmm'], $result);
	$result	= str_replace('MONTH', $arr_result['month'], $result);
	$result	= str_replace('MON', $arr_result['mon'], $result);
	$result	= str_replace('BULAN', $arr_result['bulan'], $result);
	$result	= str_replace('YYYY', $arr_result['yyyy'], $result);
	$result	= str_replace('RR', $arr_result['rr'], $result);
	$result	= str_replace('HH', $arr_result['hh'], $result);
	$result	= str_replace('MI', $arr_result['mi'], $result);
	$result	= str_replace('SS', $arr_result['ss'], $result);
	
	return trim($result);
}}



function get_time_difference( $start, $end )
{
    $uts['start']      =    strtotime( $start );
    $uts['end']        =    strtotime( $end );
    if( $uts['start']!==-1 && $uts['end']!==-1 )
    {
        if( $uts['end'] >= $uts['start'] )
        {
            $diff    =    $uts['end'] - $uts['start'];
            if( $days=intval((floor($diff/86400))) )
                $diff = $diff % 86400;
            if( $hours=intval((floor($diff/3600))) )
                $diff = $diff % 3600;
            if( $minutes=intval((floor($diff/60))) )
                $diff = $diff % 60;
            $diff    =    intval( $diff );            
            return( array('days'=>$days, 'hours'=>$hours, 'minutes'=>$minutes, 'seconds'=>$diff) );
        }
        else
        {
            trigger_error( "Ending date/time is earlier than the start date/time", E_USER_WARNING );
        }
    }
    else
    {
        trigger_error( "Invalid date/time data detected", E_USER_WARNING );
    }
    return( false );
}

function time_difference( $start, $end, $lang='en'){
	$arr_diff	= get_time_difference( $start, $end );
	$ls_return	= '';
	
	if($arr_diff['days'] > 7){
		$ls_return = date_format3($start, 'DD/MM/YYYY').' at '.date_format3($start, 'HH:MI:SS');
	}else
	if($arr_diff['days'] > 1){
		if($lang=='id'){
			$ls_return = $arr_diff['days'].' hari yg lalu';
		}else{
			$ls_return = $arr_diff['days'].' days ago';
		}
	}else
	if($arr_diff['days'] == 1){
		if($lang=='id'){
			$ls_return = 'Kemarin pada '.date_format3($start, 'HH:MI:SS');
		}else{
			$ls_return = 'Yesterday at '.date_format3($start, 'HH:MI:SS');
		}
	}else
	if($arr_diff['hours'] > 0){
		if($lang=='id'){
			$ls_return = $arr_diff['hours'].' jam yg lalu';
		}else{
			$ls_return = $arr_diff['hours'].' hours ago';
		}
	}else
	if($arr_diff['minutes'] > 0){
		if($lang=='id'){
			$ls_return = $arr_diff['minutes'].' menit yg lalu';
		}else{
			$ls_return = $arr_diff['minutes'].' minutes ago';
		}
	}else
	if($arr_diff['seconds'] > 0){
		if($lang=='id'){
			$ls_return = 'beberapa saat lalu';
		}else{
			$ls_return = 'moments ago';
		}
	}
	
	return $ls_return;
}

//$strdate	= '15/02/2008';
//$strdate	= '2008/02/15';
//$strdate	= '15/JAN/2008';
//$strdate	= '2008/JAN/15';
//$strdate	= '15/JAN/49';

//$strdate	= '15-02-2008';
//$strdate	= '2008-02-15';
//$strdate	= '15-JAN-2008';
//$strdate	= '2008-JAN-15 23:30';
//$strdate	= '49/JAN/15';

//echo date_format($strdate, 'DAY, HARI, BULAN, DD/MM/YYYY hh:mi:ss');

//echo '<hr />'.date("l , N, w, d/m/Y", strtotime('2009-08-09', date("Y-m-d")));
/*
	DAY			=> return [ 'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday' ]
	HARI		=> return [ 'Minggu', 'Senin', 'Selasa', 'Rabu', 'Kamis', 'Jumat', 'Sabtu' ]
	DD			=> return date
	MM			=> return month
	YYYY		=> return 4 digit year
	RR			=> return 2 digit year
	
	$arr_mon	= array('', 'JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL', 'AUG', 'SEP', 'OCT', 'NOV', 'DEC');
	$arr_month	= array('', 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');
	$arr_bulan	= array('', 'Januari', 'Februari', 'Maret', 'April', 'Mei', 'Juni', 'Juli', 'Agustus', 'September', 'Oktober', 'Nopember', 'Desember');
*/
?>